package com.xqh.study.springtxjta.service;

import com.xqh.study.springtxjta.dao.CustomerRepository;
import com.xqh.study.springtxjta.domain.Customer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @ClassName CustomerServiceInCode
 * @Description 编码方式实现 事务管理
 * @Author xuqianghui
 * @Date 2019/12/30 21:33
 * @Version 1.0
 */
@Service
@Slf4j
public class CustomerServiceInCode {

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private JmsTemplate jmsTemplate;

    public Customer create(Customer customer){
        customer.setUsername("InCode:"+customer.getUsername());
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        definition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_SERIALIZABLE);
        definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        definition.setTimeout(15);
        TransactionStatus status = platformTransactionManager.getTransaction(definition);
        try {
            customerRepository.save(customer);
//            if(1 == 1){
//                throw new RuntimeException("simulate error");
//            }
            jmsTemplate.convertAndSend("customer:msg:reply", customer.getUsername());
            platformTransactionManager.commit(status);
            return customer;
        }catch (Exception e ){
            platformTransactionManager.rollback(status);
            throw e;
        }
    }

    @JmsListener(destination = "customer:msg2:new")
    public void createByListener(String name){
        Customer customer = new Customer();
        customer.setUsername("InCode-listen:"+name);
        customer.setPassword("123456");
        customer.setRole("adc");
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        definition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_SERIALIZABLE);
        definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        definition.setTimeout(15);
        TransactionStatus status = platformTransactionManager.getTransaction(definition);
        try {
            customerRepository.save(customer);
//            if(1 == 1){
//                throw new RuntimeException("simulate error");
//            }
            jmsTemplate.convertAndSend("customer:msg:reply", customer.getUsername());
            platformTransactionManager.commit(status);
        }catch (Exception e ){
            platformTransactionManager.rollback(status);
            throw e;
        }
    }

    @JmsListener(destination = "customer:msg:reply")
    public void replyListener(String msg){
        log.info("reply msg ===============> {}", msg);
    }
}
